﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Entities;
using NHibernate;
using Totoro.ORMapping;

namespace Bussiness.ModelQuery
{
    public class AddressTemplateManager
    {
        public AddressTemplateManager()
        { }
        private static AddressTemplateManager instance;
        public static AddressTemplateManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new AddressTemplateManager();
                    instance.LoadData();
                }
                return instance;
            }
        }
        Dictionary<string, DeliveryAddressTemplate> adresTempCaches = new Dictionary<string, DeliveryAddressTemplate>();
        public void LoadData()
        {
            adresTempCaches.Clear();
            ISession session = NHHelper.GetCurrentSession();
            ICriteria crt = session.CreateCriteria<DeliveryAddressTemplate>();
            IList<DeliveryAddressTemplate> tempList = null;
            tempList = crt.List<DeliveryAddressTemplate>();
            if (tempList != null)
            {
                foreach (DeliveryAddressTemplate temp in tempList)
                {
                    adresTempCaches.Add(temp.ID, temp);
                }
            }
        }
        /// <summary>
        /// 根据Id获取送货地址
        /// </summary>
        /// <param name="sId">ID</param>
        /// <returns></returns>
        public DeliveryAddressTemplate GetDeliveryAddressTemplateById(string sId)
        {
            if (adresTempCaches.ContainsKey(sId))
            {
                return adresTempCaches[sId];
            }
            return null;
        }
        /// <summary>
        /// 获取所有的送货地址
        /// </summary>
        /// <returns></returns>
        public DeliveryAddressTemplate[] GetDeliveryAddressTemplates()
        {
            return adresTempCaches.Values.ToArray();
        }
        /// <summary>
        /// 获取用户的所有送货地址
        /// </summary>
        /// <param name="sUserId">用户UserId</param>
        /// <returns></returns>
        public DeliveryAddressTemplate[] GetDeliverAddressTemplateByUserId(string sUserId)
        {
            List<DeliveryAddressTemplate> tempList = new List<DeliveryAddressTemplate>();
            foreach (DeliveryAddressTemplate temp in adresTempCaches.Values)
            {
                if (temp.UserID == sUserId)
                {
                    tempList.Add(temp);
                }
            }
            return tempList.ToArray();
        }
        /// <summary>
        /// 获取用户最近使用的地址
        /// </summary>
        /// <param name="sUserId"></param>
        /// <returns></returns>
        public DeliveryAddressTemplate GetLastUserdDeliverAddressTemplate(string sUserId)
        {
            foreach (DeliveryAddressTemplate temp in adresTempCaches.Values)
            {
                if (temp.UserID == sUserId&&temp.IsLastUsed)
                {
                    return temp;
                }
            }
            return null;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sId"></param>
        public void DeleteTemplateDetailByID(string sId)
        {
            ISession session = NHHelper.GetCurrentSession();
            DeliveryAddressTemplate order = GetDeliveryAddressTemplateById(sId);
            if (order != null)
            {
                session.Delete(order);
                adresTempCaches.Remove(sId);
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="order"></param>
        public void InsertTemplate(DeliveryAddressTemplate template)
        {
            NHHelper.SaveOjb(template);
            adresTempCaches.Add(template.ID, template);
        }
    }
}
